﻿# -*- coding: utf-8 -*-
"""
下载群文件中的 access_2021_08_30.log 文件，通过正则表达式统计访问日志中 http 返回码及对应的个数，访问的 URL 以及个数
"""
import re
from pathlib import Path


def get(p, reverse=False):
    Code = {}
    path = {}
    path_len = 0
    with open(p, 'r') as file:
        a = file.readline()
        while a:
            r = re.search('\s(\d+)\s[^\"]+\"([^\"]+)', a)
            # print(r.groups())
            c = r.group(1)
            P = r.group(2)
            if len(P) > path_len:
                path_len = len(P)
            if c in Code:
                Code[c] += 1
            else:
                Code[c] = 1
            if P != '-':
                if P in path:
                    path[P] += 1
                else:
                    path[P] = 1
            a = file.readline()
    Code = sorted(Code.items(), key=lambda i: i[1], reverse=reverse)
    path = sorted(path.items(), key=lambda i: i[1], reverse=reverse)
    # Code = dict(Code)
    # path = dict(path)
    return Code, path, path_len + 2


if __name__ == '__main__':
    p = Path('./access_2021_08_30.log')
    ret = get(p)
    print('返回码及对应的个数:')
    for i in ret[0]:
        print(i[0], '---->', i[1])
    print('—' * 30, '\nURL 以及个数:')
    for i in ret[1]:
        print('{:<{}}---->{}'.format(i[0], ret[2], i[1]))
# 完成的不错，可以考虑实现过滤url进行统计